<?php
defined('BASEPATH') or exit('No direct script access allowed');

class results_service
{
    private $CI;
    public function __construct()
    {
        $this->CI = &get_instance();
        $this->CI->load->model("Academia_job_model");
        $this->CI->load->model("Academic_report_model");
        $this->CI->load->model("Audit_model");
        $this->CI->load->model("Awards_model");
        $this->CI->load->model("Communication_model");
        $this->CI->load->model("Horizontal_project_model");
        $this->CI->load->model("Monograph_model");
        $this->CI->load->model("Paper_model");
        $this->CI->load->model("Patent_data_model");
        $this->CI->load->model("Patent_model");
        $this->CI->load->model("Research_report_model");
        $this->CI->load->model("Science_conversion_model");
        $this->CI->load->model("Technical_standard_model");
        $this->CI->load->model("Vertical_project_model");
        $this->CI->load->model("Subject_platform_model");
        $this->CI->load->model("User_model");
        $this->CI->load->library('PHPExcel/PHPExcel');        
		$this->CI->load->model("Admin_model");
        $this->CI->load ->library('ProjectService/person_data_service');
        $this->CI->load ->library('ProjectService/dept_data_service');
    }

    /**
     * 查询个人成果数据
     * @access public
     * @param  $param  参数
     * @param  $is_export 是否导出
     */
    public function personData($param, $is_export = false)
    {
        $role = explode(',', getSess()['sub_role_id']);
        array_push($role, getSess()['role_id']);
        if (isset($param['type']) && $param['type'] != -1) {
            $type=array($param['type']);
        }else{
            $type=(new ResultsType)->getAllValue();
        }
        if (isset($param['name'])) {
             $name_arr = explode(",",$param['name']);
            $result['user']                  = $this->CI->User_model->get_single(array('username' => $name_arr[0],'type'=>$name_arr[1]));
            if(!$result['user']['max_xl']){
                $result['user']['max_xl']='';
            }
            if(!$result['user']['title']){
                $result['user']['title']='';
            }
            if(!$result['user']['academic_background']){
                $result['user']['academic_background']='';
            }
            if($result['user']['birthday']){
                $result['user']['birthday_char'] =  $result['user']['birthday'];
            }else{
                $result['user']['birthday_char']='';
            }
        }

        if(isset($param['check'])){
            $check=array_keys($param['check']);
        }else{
            $check= false;
        }

           // 论文模块1
        if(in_array(ResultsType::First[XPHEnum::Value], $type)){

            $data=$this->CI->person_data_service->paper($param,$is_export,$check);
         
            $result['data'][ResultsType::First[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
            
        // 专著模块1
        if(in_array(ResultsType::Second[XPHEnum::Value], $type)){
        
            $data=$this->CI->person_data_service->monograph($param,$is_export,$check);
         
            $result['data'][ResultsType::Second[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
        
        // 专利模块1
        if(in_array(ResultsType::Third[XPHEnum::Value], $type)){
            $data=$this->CI->person_data_service->patent($param,$is_export,$check);
         
            $result['data'][ResultsType::Third[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);

        }
        

        // 获奖模块1
        if(in_array(ResultsType::Fourth[XPHEnum::Value], $type)){
           $data=$this->CI->person_data_service->awards($param,$is_export,$check);
         
            $result['data'][ResultsType::Fourth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
        

        // 研究报告、政策建议模块
        if(in_array(ResultsType::Fifth[XPHEnum::Value], $type)){
             $data=$this->CI->person_data_service->research_report($param,$is_export,$check);
         
            $result['data'][ResultsType::Fifth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
           
        }
         // 技术标准模块2
        if(in_array(ResultsType::Sixth[XPHEnum::Value], $type)){
             $data=$this->CI->person_data_service->technical_standard($param,$is_export,$check);
         
            $result['data'][ResultsType::Sixth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
            

        // 纵向课题模块2
        if(in_array(ResultsType::Seventh[XPHEnum::Value], $type)){
             $data=$this->CI->person_data_service->vertical_project($param,$is_export,$check);
         
            $result['data'][ResultsType::Seventh[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
        

        // 横向课题模块2
        if(in_array(ResultsType::Eighth[XPHEnum::Value], $type)){
            $data=$this->CI->person_data_service->horizontal_project($param,$is_export,$check);
         
            $result['data'][ResultsType::Eighth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
        

        // 科技转化情况模块2
        if(in_array(ResultsType::Ninth[XPHEnum::Value], $type)){
             $data=$this->CI->person_data_service->science_conversion($param,$is_export,$check);
         
            $result['data'][ResultsType::Ninth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
        
        // 举办、参加学术交流会、学会年会2
        // if(in_array(ResultsType::Tenth[XPHEnum::Value], $type)){
        //       $data=$this->CI->person_data_service->communication($param,$is_export,$check);
         
        //     $result['data'][ResultsType::Tenth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        // }
        

        // 学术兼职情况模块
        if(in_array(ResultsType::Twelfth[XPHEnum::Value], $type)){
             $data=$this->CI->person_data_service->academia_job($param,$is_export,$check);
         
            $result['data'][ResultsType::Twelfth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }

        // 学科平台模块2
        if(in_array(ResultsType::Thirteen[XPHEnum::Value], $type)){
             $data=$this->CI->person_data_service->subject_platform($param,$is_export,$check);
         
            $result['data'][ResultsType::Thirteen[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
        
         return $result;
    }

    public function personDataExport($param)
    {
        $data = $this->personData($param, true);

        $excel = new PHPExcel();
        //公共样式
        $excel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(11);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
        $excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(23);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

        $x_line = 0;
        $y_line = 1;

        // 单元格边框样式
        $styleThinBlackBorderOutline = array(
            'borders' => array(
                'allborders' => array( //设置全部边框
                    'style' => \PHPExcel_Style_Border::BORDER_THIN, //粗的是thick
                ),

            ),
        );
        // 个人信息表头
        $excel->setActiveSheetIndex(0)->setCellValue('A' . $y_line, '个人信息');
        $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setBold(true); //第一行是否加粗
        $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setSize(16); //第一行字体大小
        $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
        $excel->getActiveSheet()->mergeCells('A1:F1');
        $y_line++;
        $user_title = array('姓名', '出生年月', '学历', '职称', '学科', '部门');
        foreach ($user_title as $k1 => $v1) {
            $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v1);
            $excel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line)->getFont()->setBold(true);
            $x_line++;
        }
        $y_line++;
        $x_line = 0;
        $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $data['user']['username']);
        $x_line++;
        $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $data['user']['birthday_char']);
        $x_line++;
        $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $data['user']['max_xl']);
        $x_line++;
        $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $data['user']['title']);
        $x_line++;
        $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $data['user']['academic_background']);
        $x_line++;
        $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $data['user']['dept_name']);
        $x_line++;
        $excel->getActiveSheet()->getStyle('A1:F3')->applyFromArray($styleThinBlackBorderOutline);
        $y_line += 3;
        $x_line = 0;

        foreach ($data['data'] as $k => $v) {
            if (count($v['data']) != 0) {
                $title_count = count($v['table_col']) - 1;

                $border_start_y = $y_line;
                // 表名
                $excel->setActiveSheetIndex(0)->setCellValue('A' . $y_line, $v['table_name']);
                $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setBold(true); //第一行是否加粗
                $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setSize(16); //第一行字体大小
                $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); //第一行行高
                $y_line++;

                $x           = strval($this->AsciiData($x_line));
                $merge_start = $x . ($y_line - 1);
                // 表头
                foreach ($v['table_col'] as $k1 => $v1) {
                    if($k1!=0){
                        $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v1);
                        $excel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line)->getFont()->setBold(true);
                        $x_line++;
                        $x         = strval($this->AsciiData($x_line - 1));
                        $merge_end = $x . ($y_line - 1);
                    }
                }
                // 合并单元格
                $excel->getActiveSheet()->mergeCells($merge_start . ':' . $merge_end);
                $excel->getActiveSheet()->getRowDimension($y_line - 1)->setRowHeight(30);
                $excel->getActiveSheet()->getRowDimension($y_line)->setRowHeight(30);
                // var_dump($merge_start.':'.$merge_end);exit;
                $x_line = 0;
                $y_line++;

                if ($v['data']) {
                    foreach ($v['data'] as $k1 => $v1) {
                        /*$excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, ($k1 + 1), PHPExcel_Cell_DataType::TYPE_STRING);
                        $x_line++;*/
                        foreach ($v1 as $k2 => $v2) {
                            if($k2=='id'){
                                $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line,($k1+1), PHPExcel_Cell_DataType::TYPE_STRING);
                                    $x_line++;
                            }else{
                                $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v2, PHPExcel_Cell_DataType::TYPE_STRING);
                                $x_line++;
                            }
                        }
                        $border_end_y = $y_line;
                        // $x=strval($this->AsciiData($x_line));
                        // $y=strval($this->AsciiData($y_line));echo $x.'1:H3';exit;
                        $x_line = 0;
                        $y_line++;
                    }
                } else {
                    $border_end_y = $y_line;
                    $y_line++;
                }
                $y_line += 2;
                $x_line = 0;
                // $x=strval($this->AsciiData($x_line)) ;
                $title_count  = strval($this->AsciiData($title_count-1));
                $table_border = 'A' . $border_start_y . ':' . $title_count . $border_end_y;
                // var_dump($table_border);
                // var_dump($table_border);
                $excel->getActiveSheet()->getStyle($table_border)->applyFromArray($styleThinBlackBorderOutline);
            }
        }
        // exit;
        $write    = new PHPExcel_Writer_Excel2007($excel);
        $director = "upload/export/personData/" . date('Ymd', time()) . "/";
        if (!file_exists($director)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($director, 0777);
        }
        $file_name = '个人成果查询' . date('Ymd') . getSess()['id'] . '.xlsx';
        $write->save($director . iconv('utf-8', 'gb2312', $file_name));
        return retJson('导出成功', true, $director . $file_name);
    }

    // Ascii大写英文
    public function AsciiData($num)
    {
        $ascii = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
        return $ascii[$num];
    }

    /**
     * 查询分院成果数据
     * @access public
     * @param  $param  参数
     * @param  $is_export  是否导出
     * @param  $is_school 是否学校
     */
    public function deptData($param, $is_export = false,$is_school=false)
    {
        $role = explode(',', getSess()['sub_role_id']);
        array_push($role, getSess()['role_id']);
        $result['dept'] = $param['dept'];
        if (isset($param['type']) && $param['type'] != -1) {
            $type=array($param['type']);
        }else{
            $type=(new ResultsType)->getAllValue();
        }
        if(isset($param['check'])){
            $check=array_keys($param['check']);
        }else{
            $check= false;
        }
          // 论文模块2
        
        if(in_array(ResultsType::First[XPHEnum::Value], $type)){
          $data=$this->CI->dept_data_service->paper($param,$is_export,$is_school,$check);
            $result['data'][ResultsType::First[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
          
        }

         // 专著模块2
        if(in_array(ResultsType::Second[XPHEnum::Value], $type)){
           
             $data=$this->CI->dept_data_service->monograph($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Second[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
        
           // 专利模块2
        if(in_array(ResultsType::Third[XPHEnum::Value], $type)){
          $data=$this->CI->dept_data_service->patent($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Third[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
          

        }      

         // 获奖模块2
        if(in_array(ResultsType::Fourth[XPHEnum::Value], $type)){
            $data=$this->CI->dept_data_service->awards($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Fourth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
            
          
        }
       
          // 研究报告、政策建议模块2
        if(in_array(ResultsType::Fifth[XPHEnum::Value], $type)){
          
             $data=$this->CI->dept_data_service->research_report($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Fifth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);

        }
        
         // 技术标准模块2
        if(in_array(ResultsType::Sixth[XPHEnum::Value], $type)){
            $data=$this->CI->dept_data_service->technical_standard($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Sixth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
            
        }
        
         // 纵向课题模块2
        if(in_array(ResultsType::Seventh[XPHEnum::Value], $type)){
           
            $data=$this->CI->dept_data_service->vertical_project($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Seventh[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
        
        // 横向课题模块2
        if(in_array(ResultsType::Eighth[XPHEnum::Value], $type)){
            
             $data=$this->CI->dept_data_service->horizontal_project($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Eighth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
           
        }
        
          // 科技转化情况模块2
        if(in_array(ResultsType::Ninth[XPHEnum::Value], $type)){
           $data=$this->CI->dept_data_service->science_conversion($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Ninth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
            
        }
        
          // 举办、参加学术交流会、学会年会模块2
        if(in_array(ResultsType::Tenth[XPHEnum::Value], $type)){
           $data=$this->CI->dept_data_service->communication($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Tenth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
          
        }
          // 学术报告模块2
         if(in_array(ResultsType::Eleventh[XPHEnum::Value], $type)){
          
            $data=$this->CI->dept_data_service->academic_report($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Eleventh[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
        }
       
        // 学术兼职情况模块2
        if(in_array(ResultsType::Twelfth[XPHEnum::Value], $type)){
           
             $data=$this->CI->dept_data_service->academia_job($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Twelfth[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);
          
        }
          
        // 学科平台模块2
        if(in_array(ResultsType::Thirteen[XPHEnum::Value], $type)){
            $data=$this->CI->dept_data_service->subject_platform($param,$is_export,$is_school,$check);
         
            $result['data'][ResultsType::Thirteen[XPHEnum::Value]] = array('table_name' => $data['table_name'], 'table_col' => $data['table_col'], 'data' => $data['data']);

        }
        return $result;
    }

    public function deptDataExport($param)
    {
        $data = $this->deptData($param, true);

        $excel = new PHPExcel();
        //公共样式
        $excel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(11);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
        $excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(23);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

        $x_line = 0;
        $y_line = 1;

        // 单元格边框样式
        $styleThinBlackBorderOutline = array(
            'borders' => array(
                'allborders' => array( //设置全部边框
                    'style' => \PHPExcel_Style_Border::BORDER_THIN, //粗的是thick
                ),

            ),
        );
        // 个人信息表头
        // $excel->setActiveSheetIndex(0)->setCellValue('A' . $y_line, '个人信息');
        // $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setBold(true); //第一行是否加粗
        // $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setSize(16); //第一行字体大小
        // $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
        // $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $data['dept']);
        // $y_line += 2;

        foreach ($data['data'] as $k => $v) {
            if (count($v['data']) > 0) {
                $title_count = count($v['table_col']) - 1;

                $border_start_y = $y_line;
                // 表名
                $excel->setActiveSheetIndex(0)->setCellValue('A' . $y_line, $data['dept'].'-'.$v['table_name']);
                $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setBold(true); //第一行是否加粗
                $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setSize(16); //第一行字体大小
                $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); //第一行行高
                $y_line++;

                $x           = strval($this->AsciiData($x_line));
                $merge_start = $x . ($y_line - 1);
                // 表头
                foreach ($v['table_col'] as $k1 => $v1) {
                     if($k1!=0){
                        $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v1);
                        $excel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line)->getFont()->setBold(true);
                        $x_line++;
                        $x         = strval($this->AsciiData($x_line - 1));
                        $merge_end = $x . ($y_line - 1);    
                     }
                   
                }
                // 合并单元格
                $excel->getActiveSheet()->mergeCells($merge_start . ':' . $merge_end);
                $excel->getActiveSheet()->getRowDimension($y_line - 1)->setRowHeight(30);
                $excel->getActiveSheet()->getRowDimension($y_line)->setRowHeight(30);
                // var_dump($merge_start.':'.$merge_end);exit;
                $x_line = 0;
                $y_line++;
                // if(count($v['data'])>0){
                foreach ($v['data'] as $k1 => $v1) {
                    $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, ($k1 + 1), PHPExcel_Cell_DataType::TYPE_STRING);
                    $x_line++;
                    foreach ($v1 as $k2 => $v2) {
                        if($k2!='id'){
                            $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v2, PHPExcel_Cell_DataType::TYPE_STRING);
                            $x_line++;
                        }
                    }
                    $border_end_y = $y_line;
                    // $x=strval($this->AsciiData($x_line));
                    // $y=strval($this->AsciiData($y_line));echo $x.'1:H3';exit;
                    $x_line = 0;
                    $y_line++;
                }
                // }else{
                //     $border_end_y=$y_line;
                //     $y_line++;
                // }
                $y_line += 2;
                $x_line = 0;
                // $x=strval($this->AsciiData($x_line)) ;
                $title_count  = strval($this->AsciiData($title_count-1));
                $table_border = 'A' . $border_start_y . ':' . $title_count . $border_end_y;
                $excel->getActiveSheet()->getStyle($table_border)->applyFromArray($styleThinBlackBorderOutline);
            }
        }
        $write    = new PHPExcel_Writer_Excel2007($excel);
        $director = "upload/export/deptData/" . date('Ymd', time()) . "/";
        if (!file_exists($director)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($director, 0777);
        }
        $file_name = '部门成果查询' . date('Ymd') . getSess()['id'] . '.xlsx';
        $write->save($director . iconv('utf-8', 'gb2312', $file_name));
        return retJson('导出成功', true, $director . $file_name);
    }



    /**
     * 查询分院成果数据
     * @access public
     * @param  $param  参数
     */
    public function schoolData($param, $is_export = false)
    {	

        $role = explode(',', getSess()['sub_role_id']);
        array_push($role, getSess()['role_id']);

        if(in_array(WebRole::SchoolLeader[XPHEnum::Value], $role)||in_array(WebRole::Sscientifictaff[XPHEnum::Value], $role)){
            $data = array();
            foreach ((new Dept())->getAll() as $k => $v) {
                $data[$k]['dept_name'] = $v[XPHEnum::Caption];
            }
        }else{

           $connar['items']="dept_name";
            $connar['groupby']="dept_name";
            $dept=(new Dept)->getAllOneD();
            foreach ($dept as $k => $v) {
                if($k==0){
                    $dept_char="'".$v."'";
                }else{
                    $dept_char.=",'".$v."'";
                }
            }
            $connar['where']=" dept_name in(".$dept_char.")";
            $data=$this->CI->Admin_model->get_list_full($connar); 
        }

    	if(isset($param['dept']) && $param['dept']!=-1){
    		$result[0]=$this->deptData($param,true,true);
    	}else{
              foreach ($data as $k => $v) {
                $param['dept']=$v['dept_name'];
                $result[$k]=$this->deptData($param,true,true); 
            }
    		
    	} 
        return $result;
    //	return 	retJson('获取学校成果数据', true, $result);
    }

    public function schoolDataExport($param)
    {
        // $data = $this->schoolData($param, true);

        $excel = new PHPExcel();
        //公共样式
        $excel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(11);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
        $excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(50);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

        $x_line = 0;
        $y_line = 1;

        // 单元格边框样式
        $styleThinBlackBorderOutline = array(
            'borders' => array(
                'allborders' => array( //设置全部边框
                    'style' => \PHPExcel_Style_Border::BORDER_THIN, //粗的是thick
                ),

            ),
        );

         $role = explode(',', getSess()['sub_role_id']);
        array_push($role, getSess()['role_id']);

        if(in_array(WebRole::SchoolLeader[XPHEnum::Value], $role)||in_array(WebRole::Sscientifictaff[XPHEnum::Value], $role)){
            $data = array();
            foreach ((new Dept())->getAll() as $k => $v) {
                $data[$k]['dept_name'] = $v[XPHEnum::Caption];
            }
        }else{
            $connar['items']="dept_name";
            $connar['groupby']="dept_name";
            $dept=(new Dept)->getAllOneD();
            foreach ($dept as $k => $v) {
                if($k==0){
                    $dept_char="'".$v."'";
                }else{
                    $dept_char.=",'".$v."'";
                }
            }
            $connar['where']=" dept_name in(".$dept_char.")";
            $data=$this->CI->Admin_model->get_list_full($connar);
        }

        

    	if(isset($param['dept']) && $param['dept']!=-1){
			$result=$this->deptData($param,true); 
			// 个人信息表头
			$excel->setActiveSheetIndex(0)->setCellValue('A' . $y_line, '个人信息');
			$excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setBold(true); //第一行是否加粗
			$excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setSize(16); //第一行字体大小
			$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
			$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $result['dept']);
			$y_line += 2;

			foreach ($result['data'] as $k => $v) {				
			    if (count($v['data']) > 0) {
			        $title_count = count($v['table_col']) - 1;

			        $border_start_y = $y_line;
			        // 表名
			        $excel->setActiveSheetIndex(0)->setCellValue('A' . $y_line, $v['table_name']);
			        $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setBold(true); //第一行是否加粗
			        $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setSize(16); //第一行字体大小
			        $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); //第一行行高
			        $y_line++;

			        $x           = strval($this->AsciiData($x_line));
			        $merge_start = $x . ($y_line - 1);
			        // 表头
			        foreach ($v['table_col'] as $k1 => $v1) {
                        if($k1!=0){
                            $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v1);
                            $excel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line)->getFont()->setBold(true);
                            $x_line++;
                            $x         = strval($this->AsciiData($x_line - 1));
                            $merge_end = $x . ($y_line - 1);
                        }
			        }
			        // 合并单元格
			        $excel->getActiveSheet()->mergeCells($merge_start . ':' . $merge_end);
			        $excel->getActiveSheet()->getRowDimension($y_line - 1)->setRowHeight(30);
			        $excel->getActiveSheet()->getRowDimension($y_line)->setRowHeight(30);
			        // var_dump($merge_start.':'.$merge_end);exit;
			        $x_line = 0;
			        $y_line++;
			        // if(count($v['data'])>0){
			        foreach ($v['data'] as $k1 => $v1) {
			            // $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, ($k1 + 1), PHPExcel_Cell_DataType::TYPE_STRING);
			            // $x_line++;
			            foreach ($v1 as $k2 => $v2) {
			                if($k2=='id'){
                                $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line,($k1+1), PHPExcel_Cell_DataType::TYPE_STRING);
                                $x_line++;
                            }else{
                                $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v2, PHPExcel_Cell_DataType::TYPE_STRING);
                                $x_line++;
                            }
			            }
			            $border_end_y = $y_line;
			            $x_line = 0;
			            $y_line++;
			        }
			        $y_line += 2;
			        $x_line = 0;
			        // $x=strval($this->AsciiData($x_line)) ;
			        $title_count  = strval($this->AsciiData($title_count-1));
			        $table_border = 'A' . $border_start_y . ':' . $title_count . $border_end_y;
			        $excel->getActiveSheet()->getStyle($table_border)->applyFromArray($styleThinBlackBorderOutline);
			    }
			}
    	}else{
    		foreach ($data as $k => $v) {
    			$param['dept']=$v['dept_name'];
    			$result=$this->deptData($param,true); 
    			// 个人信息表头
    			// $excel->setActiveSheetIndex(0)->setCellValue('A' . $y_line, '个人信息');
    			// $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setBold(true); //第一行是否加粗
    			// $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setSize(16); //第一行字体大小
    			// $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
    			// $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $result['dept']);
    			// $y_line += 2;
    			foreach ($result['data'] as $k => $v) {
    				
    			    if (count($v['data']) > 0) {
    			        $title_count = count($v['table_col']) - 1;

    			        $border_start_y = $y_line;
    			        // 表名
    			        $excel->setActiveSheetIndex(0)->setCellValue('A' . $y_line, $result['dept'].'-'.$v['table_name']);
    			        $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setBold(true); //第一行是否加粗
    			        $excel->getActiveSheet()->getStyle('A' . $y_line)->getFont()->setSize(16); //第一行字体大小
    			        $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); //第一行行高
    			        $y_line++;

    			        $x           = strval($this->AsciiData($x_line));
    			        $merge_start = $x . ($y_line - 1);
    			        // 表头
    			        foreach ($v['table_col'] as $k1 => $v1) {
                            if($k1!=0){
                                $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v1);
                                $excel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line)->getFont()->setBold(true);
                                $x_line++;
                                $x         = strval($this->AsciiData($x_line - 1));
                                $merge_end = $x . ($y_line - 1);
                            }
    			        }
    			        // 合并单元格
    			        $excel->getActiveSheet()->mergeCells($merge_start . ':' . $merge_end);
    			        $excel->getActiveSheet()->getRowDimension($y_line - 1)->setRowHeight(30);
    			        $excel->getActiveSheet()->getRowDimension($y_line)->setRowHeight(30);
    			        // var_dump($merge_start.':'.$merge_end);exit;
    			        $x_line = 0;
    			        $y_line++;
    			        // if(count($v['data'])>0){
    			        foreach ($v['data'] as $k1 => $v1) {
    			            // $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, ($k1 + 1), PHPExcel_Cell_DataType::TYPE_STRING);
    			            // $x_line++;
    			            foreach ($v1 as $k2 => $v2) {
                                if($k2=='id'){
                                    $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line,($k1+1), PHPExcel_Cell_DataType::TYPE_STRING);
                                    $x_line++;
                                }else{
                                    $excel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v2, PHPExcel_Cell_DataType::TYPE_STRING);
                                    $x_line++;
                                }
    			                
    			            }
    			            $border_end_y = $y_line;
    			            $x_line = 0;
    			            $y_line++;
    			        }
    			        $y_line += 2;
    			        $x_line = 0;
    			        // $x=strval($this->AsciiData($x_line)) ;
    			        $title_count  = strval($this->AsciiData($title_count-1));
    			        $table_border = 'A' . $border_start_y . ':' . $title_count . $border_end_y;
    			        $excel->getActiveSheet()->getStyle($table_border)->applyFromArray($styleThinBlackBorderOutline);
    			    }
    			}

    		}
    	}
    	// var_dump(expression)
        // exit;


        $write    = new PHPExcel_Writer_Excel2007($excel);
        $director = "upload/export/schoolData/" . date('Ymd', time()) . "/";
        if (!file_exists($director)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($director, 0777);
        }
        $file_name = '成果查询' . date('Ymd') . getSess()['id'] . '.xlsx';
        $write->save($director . iconv('utf-8', 'gb2312', $file_name));
        return retJson('导出成功', true, $director . $file_name);
    }

}
